import com.mit.web.business.proxy.PurchaseProxy;
import com.mit.web.business.remote.IInvokeResponder;
import com.mit.web.event.PopWinCloseEvent;
import com.mit.web.model.purchase.SupplierEnquiryModel;
import com.mit.web.util.LogUtil;
import com.mit.web.util.MessageUtil;
import com.mit.web.view.component.monitor.IProgressMonitor;
import com.mit.web.view.purchase.SupplierWindow;
import com.mit.web.vo.purchase.SupplierVO;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import mx.events.ListEvent;
import mx.logging.ILogger;
import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;

private var _pm:IProgressMonitor;
private var _proxy:PurchaseProxy = PurchaseProxy.getInstance();
private var _logger:ILogger;
[Bindable] private var _model:SupplierEnquiryModel;


/**
 * 为datagrid加载数据
 * */
private function datagridCreationCompleteHanlder(event:FlexEvent):void{
	_logger = LogUtil.getLogger(this);
	_logger.debug("DataGrid created, load all records");
	
	_model = new SupplierEnquiryModel();
	doEnquiry();
}

private function doEnquiry(name:String = null, pageNo:int = 1):void{
	//读取记录集
	var r1:IInvokeResponder = _proxy.getPagedSuppliers(pageNo, name);
	r1.addResultListener(retrieveListHandler);
	r1.addFaultListener(exceptionHandler);
	
	var r2:IInvokeResponder = _proxy.getSupplierCount(name);
	r2.addResultListener(retrieveRCHandler);
	r2.addFaultListener(exceptionHandler);
}

private function retrieveListHandler(event:ResultEvent):void{
	var lst:ArrayCollection = event.result as ArrayCollection;
	//有记录返回才更新，否则只是提示无记录存在
	if(lst != null && lst.length > 0){
		_model.supplierLst = lst;
		dg.validateNow();
	}
	else
		MessageUtil.popNoRecordMessage(this);
}

private function retrieveRCHandler(event:ResultEvent):void{
	var rc:int = event.result as int;
	//与取回记录的行为保持一致，如果没有记录找到，不做任何动作。
	if(rc > 0){
		_logger.debug("Total record: " + rc.toString());
		_model.recordCount = rc;
	}
}
/**
 * 双击记录
 * */
private function doEdit(event:ListEvent):void{
	popWin(dg.selectedItem as SupplierVO);
}

/**
 * 输入搜索条件后按回车键
 * */
private function doSearch():void{
	//跳回第一页
	_model.pageNo = 1;
	_model.searchName =txt_search.text;	
	doEnquiry(_model.searchName);
}

/**
 * 点击刷新按钮
 * */
private function doRefresh(event:MouseEvent):void{
	_model.searchName = null;
	_model.pageNo = 1;
	doEnquiry();
}

/**
 * 翻页
 * */
private function goToPage(pageNo:int):void{
	_model.pageNo = pageNo;
	doEnquiry(_model.searchName, _model.pageNo);
}


/**
 * 新建供应商
 * */
private function newSupplier(event:MouseEvent):void{
	var supplier:SupplierVO = new SupplierVO();
	popWin(supplier);
}

private function popWin(supplier:SupplierVO):void{
	var window:SupplierWindow = new SupplierWindow();
	window.addEventListener(PopWinCloseEvent.SAVED, editRecordHandler);
	window.addEventListener(PopWinCloseEvent.CREATED, createRecordHandler);
	if(supplier.id > 0)
		window.title = supplier.name;
	window.supplier = supplier;
	PopUpManager.addPopUp(window,this,true);
	PopUpManager.centerPopUp(window);
}

private function editRecordHandler(event:PopWinCloseEvent):void{
	//直接用保存后的VO替换，不重新读取数据库
	var savedSupplier:SupplierVO = event.vo as SupplierVO;
	var src:Array = _model.supplierLst.source;
	src[dg.selectedIndex] =  savedSupplier;
	var updatedLst:ArrayCollection = new ArrayCollection(src);
	_model.supplierLst = updatedLst;
	dg.validateNow();
	//显示成功提示
	showMessageTip(event.msg, dg);
}

private function createRecordHandler(event:PopWinCloseEvent):void{
	//新建的VO插入第一个元素
	var src:Array = _model.supplierLst.source;
	var savedSupplier:SupplierVO = event.vo as SupplierVO;
	src.unshift(savedSupplier);
	//如果一页满了就移除最后一个记录-- pending，it may be a bad behavior
	//src.pop();
	var updatedLst:ArrayCollection = new ArrayCollection(src);
	_model.supplierLst = updatedLst;
	dg.validateNow();
	//显示成功提示
	showMessageTip(event.msg, dg);
}
